package com.example.erat.controller;

import com.example.erat.dto.CourseDTO;
import com.example.erat.dto.ExperimentDTO;
import com.example.erat.dto.ResponseResult;
import com.example.erat.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 课程控制器
 */
@RestController
@RequestMapping("/api/courses")
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 创建课程
     */
    @PostMapping
    public ResponseResult<CourseDTO> createCourse(@Valid @RequestBody CourseDTO courseDTO) {
        return ResponseResult.success(courseService.createCourse(courseDTO));
    }

    /**
     * 更新课程
     */
    @PutMapping("/{id}")
    public ResponseResult<CourseDTO> updateCourse(@PathVariable Long id, @Valid @RequestBody CourseDTO courseDTO) {
        courseDTO.setId(id);
        return ResponseResult.success(courseService.updateCourse(courseDTO));
    }

    /**
     * 删除课程
     */
    @DeleteMapping("/{id}")
    public ResponseResult<Void> deleteCourse(@PathVariable Long id) {
        courseService.deleteCourse(id);
        return ResponseResult.success();
    }

    /**
     * 获取课程详情
     */
    @GetMapping("/{id}")
    public ResponseResult<CourseDTO> getCourse(@PathVariable Long id) {
        return ResponseResult.success(courseService.getCourseById(id));
    }

    /**
     * 获取课程列表
     */
    @GetMapping
    public ResponseResult<List<CourseDTO>> listCourses() {
        return ResponseResult.success(courseService.listCourses());
    }

    /**
     * 添加实验
     */
    @PostMapping("/{id}/experiments")
    public ResponseResult<ExperimentDTO> addExperiment(@PathVariable Long id, @Valid @RequestBody ExperimentDTO experimentDTO) {
        return ResponseResult.success(courseService.addExperiment(id, experimentDTO));
    }

    /**
     * 更新实验
     */
    @PutMapping("/experiments/{id}")
    public ResponseResult<ExperimentDTO> updateExperiment(@PathVariable Long id, @Valid @RequestBody ExperimentDTO experimentDTO) {
        experimentDTO.setId(id);
        return ResponseResult.success(courseService.updateExperiment(experimentDTO));
    }

    /**
     * 删除实验
     */
    @DeleteMapping("/experiments/{id}")
    public ResponseResult<Void> deleteExperiment(@PathVariable Long id) {
        courseService.deleteExperiment(id);
        return ResponseResult.success();
    }

    /**
     * 获取课程实验列表
     */
    @GetMapping("/{id}/experiments")
    public ResponseResult<List<ExperimentDTO>> listCourseExperiments(@PathVariable Long id) {
        return ResponseResult.success(courseService.listCourseExperiments(id));
    }
} 